package com.xk857.util.aliyun;


import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

/**
 * @author CV大魔王
 * @Description 阿里云短信发送工具类
 * @date 2021/2/28 20:55
 */
public class SMSUtils {

    public static final String REGISTER_CODE = "SMS_212275656";//发送短信注册验证码
    public static final String CHANGE_PWD_CODE = "SMS_212275663";//更改密码验证码

    private String accessKeyId = "LTAI4GCckVJucWYjDyaZTagV";

    private String accessKeySecret = "ufttHGnIkXXRJst7LezXUS9Qh2OHiT";

    //对应签名名称
    private static final String signname = "星空小屋";

    /**
     * 短信发送
     *
     * @param telPhone     手机号码
     * @param templateCode 模板CODE
     * @param code         验证码
     */
    public void SendMsg(String telPhone, String templateCode, String code) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        //aliyun对应发送短信的服务器地址
        request.setDomain("dysmsapi.aliyuncs.com");
        //对应版本号
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", telPhone);
        request.putQueryParameter("SignName", signname);
        request.putQueryParameter("TemplateCode", templateCode);
        request.putQueryParameter("TemplateParam", "{\"code\":" + code + "}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());//返回信息
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }

}
